package cn.exrick.xboot.modules.assessment.serviceimpl;


import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.assessment.dao.mapper.HkIndexClassifyMapper;
import cn.exrick.xboot.modules.assessment.entity.HkIndexClassify;
import cn.exrick.xboot.modules.assessment.service.IHkIndexClassifyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2022-08-30
 */
@Service
@Transactional
public class HkIndexClassifyServiceImpl extends ServiceImpl<HkIndexClassifyMapper, HkIndexClassify> implements IHkIndexClassifyService {


    @Autowired
    private HkIndexClassifyMapper hkIndexClassifyMapper;

    @Override
    public IPage<HkIndexClassify> selectPage(Page<HkIndexClassify> page, HkIndexClassify hkIndexClassify) {
        QueryWrapper<HkIndexClassify> query = Wrappers.query();
        query.lambda().like(StringUtils.isNotBlank(hkIndexClassify.getIndexClassifyName()),HkIndexClassify::getIndexClassifyName,hkIndexClassify.getIndexClassifyName());
        QueryWrapper<HkIndexClassify> entityWrapper = getEntityWrapper(query, hkIndexClassify);
        return hkIndexClassifyMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HkIndexClassify> selectList(HkIndexClassify hkIndexClassify) {
        QueryWrapper<HkIndexClassify> query = Wrappers.query();
        QueryWrapper<HkIndexClassify> entityWrapper = getEntityWrapper(query, hkIndexClassify);
        return hkIndexClassifyMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HkIndexClassify> getEntityWrapper(QueryWrapper<HkIndexClassify> entityWrapper, HkIndexClassify hkIndexClassify) {
        //条件拼接
        return entityWrapper;
    }

    @Override
    public Result<Object> saveHkIndexClassify(String indexClassifyName){
        QueryWrapper<HkIndexClassify> query = Wrappers.query();
        query.lambda().eq(StringUtils.isNotBlank(indexClassifyName),HkIndexClassify::getIndexClassifyName,indexClassifyName);
        HkIndexClassify hkIndexClassifyByName = hkIndexClassifyMapper.selectOne(query);
        if (hkIndexClassifyByName != null){
            return ResultUtil.error("该类别已存在");
        }
        HkIndexClassify hkIndexClassify = new HkIndexClassify();
        hkIndexClassify.setIndexClassifyName(indexClassifyName);
        hkIndexClassifyMapper.insert(hkIndexClassify);
        return ResultUtil.success("添加成功");
    }
}
